package com.zhou.server.impl;

import com.zhou.dao.BookDao;
import com.zhou.dao.UserDao;
import com.zhou.domain.BookTest;
import com.zhou.domain.DTO.BorrowBooksDTO;
import com.zhou.domain.Book;
import com.zhou.domain.DTO.ReturnBookDTO;
import com.zhou.domain.Rusult;
import com.zhou.domain.User;
import com.zhou.enumx.BookStatus;
import com.zhou.exception.BaseException;
import com.zhou.exception.ErrorEnum;
import com.zhou.server.BookServer;
import com.zhou.util.JsonUtil;
import com.zhou.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServerImpl implements BookServer {

    @Autowired
    BookDao bookDao;

    @Autowired
    UserDao userDao;

    //借书
    @Override
//    @Transactional
    public Rusult borrowBooks(BorrowBooksDTO borrowBooksDTO) {
        Book book = bookDao.selectById(borrowBooksDTO.getBookId());
        if (book==null){
            throw new BaseException(ErrorEnum.NO_BOOK.getMsg());
        }
        //没有库存
        if (book.getEnStatus().getSign()!=1){
            throw new BaseException(ErrorEnum.NO_STOCK.getMsg());
        }
        //图书库存-1
        if (book.getStock()-1==0){
            book.setEnStatus(BookStatus.NO_STOCK);
        }
            book.setStock(book.getStock()-1);
            bookDao.updeta(book);
        //用户借书更改
        User user = userDao.getById(borrowBooksDTO.getUserId());
        try {
            List deserialize = JsonUtil.deserialize(user.getBookListJson(), List.class);
            user.setBorrowedBooks(deserialize);
            user.getBorrowedBooks().add(book);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            user.setBookListJson(JsonUtil.serialize(user.getBorrowedBooks()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        userDao.updeta(user);
        return new Rusult(200,null,"借书成功");
    }
    //还书
    @Override
    public String returnBook(ReturnBookDTO returnBookDTO) {
        User user = userDao.getById(returnBookDTO.getUserId());
        try {
            List<LinkedHashMap> deserialize = JsonUtil.deserialize(user.getBookListJson(), List.class);
            Object id = deserialize.get(0).get("id");
            for (int i = 0; i < deserialize.size(); i++) {
                //int id = (int)ObjectUtil.getFieldValueByName("id", deserialize.get(i));

                if((int)(deserialize.get(i).get("id"))==returnBookDTO.getBookId()){
                    deserialize.remove(i);
                }
            }
            user.setBookListJson(JsonUtil.serialize(deserialize));
            userDao.updeta(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Book book = bookDao.selectById(returnBookDTO.getBookId());
        book.setStock(book.getStock()+1);
        if (book.getStock()!=0){
            book.setEnStatus(BookStatus.IN_STOCK);
        }
        bookDao.updeta(book);
        return "还书成功";
    }

}
























